package com.yeheng;

import java.util.LinkedList;

/**
 * @Classname Slow
 * @Description :
 * @Date 2021/2/27 11:15
 * @Created by Yeheng
 */
public class SlowHashSet<T> {

    private LinkedList<T>[] arr =new LinkedList[100];
    //这个模拟就是HashSet在存储结构，连续的是数组，数组每个部门都是一个链表集合

    public void add(T t) {
        int hc = t.hashCode();

        // 计算出一个桶位
        //就是计算出一个元素可能出现的区域，就是可能的下标，然后去链表集合里找
        int i = hc % arr.length;

        if(arr[i] == null) {
            arr[i] = new LinkedList<>();
            arr[i].add(t);
        } else {
            int j;
            for (j = 0; j < arr[i].size(); j++) {
                if(t.equals(arr[i].get(j))) {
                    break;
                }
            }
            if(j == arr[i].size()) {
                arr[i].add(t);
            }
        }
    }

    // contains
    public boolean contains(T t){
        if (t==null){
            return false;
        }
        int hc = t.hashCode();
        int i = hc%arr.length;
        if (i<0||i> arr.length-1){
            return false;
        }
        if (arr[i]==null){
            return false;
        }
        boolean flag = false;
        for (int j = 0; j < arr[i].size(); j++) {
            if (arr[i].get(j).equals(t)){
                flag = true;
                break;
            }
        }
        return flag;
    }

    // remove
    public T remove(T t){
        if (t == null){
            return null;
        }
        int hc = t.hashCode();
        int i = hc%arr.length;
        if (i<0||i> arr.length-1){
            return null;
        }
        if (arr[i]==null){
            return null;
        }
        T t2 = null;
        for (int j = 0; j < arr[i].size(); j++) {
            if (arr[i].get(j).equals(t)){
                t2 = arr[i].get(j);
                arr[i].remove(j);
                break;
            }
        }
        return t2;
    }

    public int size() {
        int size = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] != null) {
                size += arr[i].size();
            }
        }
        return size;
    }
}
class AppA{
    public static void main(String[] args) {

    }
}

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}
